using ASP;
using HoWave.Common;
using HoWave.Data.Bll;
using HoWave.Data.Model;
using HoWave.Web;
using HoWave.Web.Shop.Member;
using System;
using System.Data;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Member_JXC_AccountAoucherInfo : Member, IRequiresSessionState
{
	private string action = "";
	private int infoID = 0;
	private Shop_Product _shop_product = null;
	protected ScriptManager ScriptManager1;
	protected Literal ltrTitle;
	protected TextBox txtAccountANumber;
	protected TextBox txtAccountACountAdd;
	protected RegularExpressionValidator revCountAdd;
	protected TextBox txtInputer;
	protected TextBox txtCreateTime;
	protected Label lblCauditName;
	protected Label lblCauditDate;
	protected HtmlTableRow tr_IsCaudit;
	protected Label lblAccountName;
	protected Label lblAccountDate;
	protected HtmlTableRow tr_IsAccount;
	protected TextBox txtAccountAExplainAdd;
	protected HiddenField hfInfoID;
	protected HtmlGenericControl divItem;
	protected HiddenField hfStockOrderID;
	protected HtmlGenericControl divInfo;
	protected Button btnCashierOK;
	protected Button btnCashierCancle;
	protected Button btnDelInfo;
	protected Button btnSave;
	protected Button btnCancle;
	public string Action
	{
		get
		{
			if (base.Request.QueryString["Action"] != null)
			{
				this.action = base.Request.QueryString["Action"].Trim();
			}
			else
			{
				this.action = "";
			}
			return this.action;
		}
		set
		{
			this.action = value;
		}
	}
	public int InfoID
	{
		get
		{
			if (base.Request.QueryString["InfoID"] != null)
			{
				this.infoID = Convert.ToInt32(base.Request.QueryString["InfoID"].Trim());
			}
			return this.infoID;
		}
		set
		{
			this.infoID = value;
		}
	}
	protected Shop_Product shop_product
	{
		get
		{
			Shop_Product shop_product;
			if (this._shop_product == null)
			{
				this._shop_product = new Shop_Product(this.conn);
				shop_product = this._shop_product;
			}
			else
			{
				shop_product = this._shop_product;
			}
			return shop_product;
		}
	}
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!base.IsPostBack)
		{
			if (!base.OwnerPowerFinancemanage(base.user.UserID))
			{
				base.WriteRightErrMsg();
			}
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (text == "editinfo")
					{
						this.ltrTitle.Text = "修改记账凭证";
						this.btnCancle.Text = "返回";
						this.btnSave.Text = "修改";
						this.btnSave.Enabled = true;
						this.txtAccountANumber.Enabled = false;
						this.txtInputer.Enabled = false;
						this.txtCreateTime.Enabled = false;
						if (this.InfoID > 0)
						{
							this.InitPage();
						}
						else
						{
							this.WriteErrMsg("", "没有凭证信息！", "CheckAccountAoucher.aspx");
						}
					}
				}
				else
				{
					this.ltrTitle.Text = "添加记账凭证";
					this.txtAccountANumber.Text = "JZPZ" + DateTime.Now.TimeString();
					this.txtAccountANumber.Enabled = false;
					this.txtCreateTime.Text = DateTime.Now.ToString();
					this.txtInputer.Text = base.user.UserName;
					this.btnDelInfo.Enabled = false;
					this.btnCashierOK.Enabled = false;
					this.btnCashierCancle.Enabled = false;
				}
			}
		}
	}
	private bool CheckInput()
	{
		return true;
	}
	protected void InitPage()
	{
		HoWave.Data.Model.AccountAoucher model = BLLInstance.BllAccountAoucher.GetModel(this.InfoID);
		if (model != null)
		{
			this.hfInfoID.Value = this.InfoID.ToString();
			this.txtAccountANumber.Text = model.AccountANumber;
			this.txtAccountACountAdd.Text = model.AccountACount.ToString();
			this.txtAccountAExplainAdd.Text = model.AccountAExplain;
			this.txtInputer.Text = model.Inputer;
			this.txtCreateTime.Text = model.InputDate.ToString();
			string text = model.IsCaudit.ToString();
			if (text.ToLower().Trim() == "true")
			{
				this.btnCashierOK.Enabled = false;
				this.btnCashierCancle.Enabled = true;
				this.btnDelInfo.Enabled = false;
				this.tr_IsCaudit.Visible = true;
				this.lblCauditName.Text = model.CauditName;
				this.lblCauditDate.Text = model.CauditDate.ToString();
			}
			else
			{
				this.btnCashierOK.Enabled = true;
				this.btnCashierCancle.Enabled = false;
			}
			string text2 = model.IsAccount.ToString();
			if (text2.ToLower().Trim() == "true")
			{
				this.tr_IsAccount.Visible = true;
				this.lblAccountName.Text = model.AccountName;
				this.lblAccountDate.Text = model.AccountDate.ToString();
			}
		}
	}
	private HoWave.Data.Model.AccountAoucher GetAccountAoucher()
	{
		HoWave.Data.Model.AccountAoucher accountAoucher = new HoWave.Data.Model.AccountAoucher();
		accountAoucher.AccountANumber = this.txtAccountANumber.Text;
		accountAoucher.AccountACount = new int?(0);
		if (!string.IsNullOrEmpty(this.txtAccountACountAdd.Text))
		{
			accountAoucher.AccountACount = new int?(Convert.ToInt32(this.txtAccountACountAdd.Text));
		}
		accountAoucher.AccountAExplain = this.txtAccountAExplainAdd.Text;
		accountAoucher.Inputer = this.txtInputer.Text;
		DateTime value = DateTime.Now;
		if (!string.IsNullOrEmpty(this.txtCreateTime.Text))
		{
			value = this.txtCreateTime.Text.ToDateTime(new DateTime[0]);
		}
		accountAoucher.InputDate = new DateTime?(value);
		accountAoucher.OwnerID = base.user.UserID;
		return accountAoucher;
	}
	protected void btnSave_Click(object sender, EventArgs e)
	{
		if (this.CheckInput())
		{
			string text = this.Action;
			if (text != null)
			{
				if (!(text == "addinfo"))
				{
					if (text == "editinfo")
					{
						HoWave.Data.Model.AccountAoucher model = BLLInstance.BllAccountAoucher.GetModel(this.InfoID);
						if (model != null)
						{
							string comeUrl = "CheckAccountAoucher.aspx?Action=checkinfo&InfoID=" + this.InfoID;
							model.AccountACount = new int?(0);
							if (!string.IsNullOrEmpty(this.txtAccountACountAdd.Text))
							{
								model.AccountACount = new int?(Convert.ToInt32(this.txtAccountACountAdd.Text));
							}
							model.AccountAExplain = this.txtAccountAExplainAdd.Text;
							model.UpdateDate = new DateTime?(DateTime.Now);
							bool flag = BLLInstance.BllAccountAoucher.Update(model);
							if (flag)
							{
								this.WriteSuccessMsg("", "修改凭证信息成功！", comeUrl);
							}
							else
							{
								this.WriteErrMsg("", "修改凭证信息失败！", comeUrl);
							}
						}
					}
				}
				else
				{
					HoWave.Data.Model.AccountAoucher accountAoucher = this.GetAccountAoucher();
					if (!string.IsNullOrEmpty(this.hfStockOrderID.Value))
					{
						int num = Convert.ToInt32(this.hfStockOrderID.Value);
						accountAoucher.StockOrderID = new int?(num);
						int num2 = BLLInstance.BllAccountAoucher.Add(accountAoucher);
						if (num2 > 0)
						{
							int num3 = this.AddAAItem(num, num2);
							if (num3 > 0)
							{
								this.WriteSuccessMsg("", "添加凭证信息成功！", "CheckAccountAoucher.aspx?Action=checkinfo&InfoID=" + num2);
							}
						}
						else
						{
							this.WriteErrMsg("", "添加凭证信息失败！", "");
						}
					}
				}
			}
		}
	}
	private int AddAAItem(int StockOrderID, int aainfoID)
	{
		int result = 0;
		DataTable stockInfoOrderByStockOrderID = BLLInstance.BllStockInfoOrder.GetStockInfoOrderByStockOrderID(StockOrderID);
		for (int i = 0; i < stockInfoOrderByStockOrderID.Rows.Count; i++)
		{
			decimal moneyGoods = 0m;
			if (stockInfoOrderByStockOrderID.Rows[i]["MoneyGoods"] != DBNull.Value)
			{
				moneyGoods = Convert.ToDecimal(stockInfoOrderByStockOrderID.Rows[i]["MoneyGoods"].ToString());
			}
			decimal deliver_TotalPrice = 0m;
			if (stockInfoOrderByStockOrderID.Rows[i]["Deliver_TotalPrice"] != DBNull.Value)
			{
				deliver_TotalPrice = Convert.ToDecimal(stockInfoOrderByStockOrderID.Rows[i]["Deliver_TotalPrice"].ToString());
			}
			decimal invoice_TotalPrice = 0m;
			if (stockInfoOrderByStockOrderID.Rows[i]["Invoice_TotalPrice"] != DBNull.Value)
			{
				invoice_TotalPrice = Convert.ToDecimal(stockInfoOrderByStockOrderID.Rows[i]["Invoice_TotalPrice"].ToString());
			}
			decimal fee = 0m;
			if (stockInfoOrderByStockOrderID.Rows[i]["Fee"] != DBNull.Value)
			{
				fee = Convert.ToDecimal(stockInfoOrderByStockOrderID.Rows[i]["Fee"].ToString());
			}
			decimal receiptMoney = 0m;
			if (stockInfoOrderByStockOrderID.Rows[i]["ReceiptMoney"] != DBNull.Value)
			{
				receiptMoney = Convert.ToDecimal(stockInfoOrderByStockOrderID.Rows[i]["ReceiptMoney"].ToString());
			}
			result = BLLInstance.BllAccountAoucherItem.AccountAoucherItemAddBySell(aainfoID, receiptMoney, moneyGoods, deliver_TotalPrice, invoice_TotalPrice, fee);
		}
		return result;
	}
	public void btnCancle_Click(object sender, EventArgs e)
	{
		base.Response.Redirect("CheckAccountAoucher.aspx");
	}
	protected void btnCashierOK_Click(object sender, EventArgs e)
	{
		HoWave.Data.Model.AccountAoucher model = BLLInstance.BllAccountAoucher.GetModel(this.InfoID);
		if (model != null)
		{
			model.AccountAoucherID = this.InfoID;
			model.IsCaudit = true;
			model.CauditName = base.user.UserName;
			model.CauditDate = new DateTime?(DateTime.Now);
			bool flag = BLLInstance.BllAccountAoucher.Update(model);
			if (flag)
			{
				this.WriteSuccessMsg("", "审核凭证信息成功！", "CheckAccountAoucher.aspx?Action=checkinfo&InfoID=" + this.InfoID);
			}
		}
	}
	protected void btnCashierCancle_Click(object sender, EventArgs e)
	{
		HoWave.Data.Model.AccountAoucher model = BLLInstance.BllAccountAoucher.GetModel(this.InfoID);
		if (model != null)
		{
			model.AccountAoucherID = this.InfoID;
			model.IsCaudit = false;
			bool flag = BLLInstance.BllAccountAoucher.Update(model);
			if (flag)
			{
				this.WriteSuccessMsg("", "取消审核凭证信息成功！", "CheckAccountAoucher.aspx?Action=checkinfo&InfoID=" + this.InfoID);
			}
		}
	}
	protected void btnDelInfo_Click(object sender, EventArgs e)
	{
		HoWave.Data.Model.AccountAoucher model = BLLInstance.BllAccountAoucher.GetModel(this.InfoID);
		if (model != null)
		{
			DataTable dataTable = BLLInstance.BllAccountAoucherItem.GetList("AccountAoucherID=" + this.InfoID).Tables[0];
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				int accountAItemID = Convert.ToInt32(dataTable.Rows[i]["AccountAItemID"].ToString());
				BLLInstance.BllAccountAoucherItem.Delete(accountAItemID);
			}
			bool flag = BLLInstance.BllAccountAoucher.Delete(this.InfoID);
			if (flag)
			{
				this.WriteSuccessMsg("", "删除凭证信息成功！", "CheckAccountAoucher.aspx?&AccountANumber=" + model.AccountANumber);
			}
		}
	}
}
